<?php 
include_once 'viper/autoloader.php';

class viper_server_autoregistration
{	
	const mass_register_number = 100;
	
	private static function _register($scores){
		if(count($scores)!=50){
			die('Need 50 feature scores');
		}
		
		self::_register_feature_score($scores);
		self::_register_quick_list();
		
		$newid = self::_count_user();
		
		return $newid;
	}
	
	private static function _register_feature_score($scores){
		//insert
		$sql = 'INSERT INTO userfeaturescore VALUES (""';
		
		foreach($scores as $score){
			$sql = $sql . ',' . $score;
		}
		
		$sql = $sql . ')';
		
		viper_dao::query($sql);
	}
	
	private static function _register_quick_list(){
		//insert
		$sql = 'INSERT INTO userquicklist VALUES (""';
		
		$scores = array();
		for($i=0;$i<10;$i++){
			$scores[] = -1;
		}
		
		foreach($scores as $score){
			$sql = $sql . ',' . $score;
		}
		
		$sql = $sql . ')';
		
		viper_dao::query($sql);
	}
	
	
	private static function _count_user(){
		$sql = 'SELECT count(*) FROM userquicklist';
		
		$number = viper_dao::query($sql);
		
		return $number[0]['count(*)'];
	}
	
	private static function _autogenerate_scores(){
		$scores = array();
		
		for($i=0; $i<50; $i++){
			$random = rand(1,1);
			
			if($random){
				$scores['feature'.$i] = rand(1,100);
			} else {
				$scores['feature'.$i] = 5;
			}
		}
		
		return $scores;
	}
	
	public static function autoregister(){
		$scores = self::_autogenerate_scores();
		
		$newid = self::_register($scores);
		
		return array($newid,$scores);
	}
	
	public static function mass_autoregister(){
		$globalaverage = array();
		
		for($i=0;$i<50;$i++){
			$globalaverage['feature'.$i] = 0;
		}
		
		for($i=0;$i<self::mass_register_number;$i++){
			$result = self::autoregister();
			$result = $result[1];
			
			foreach($result as $key => $value){
				$globalaverage[$key] = $globalaverage[$key] + $value;
			}
		}
		
		$sql = 'INSERT INTO globalaverage VALUES (';
		foreach($globalaverage as $key => $value){
			$sql = $sql . $value;
			
			if($key != 'feature49') $sql = $sql . ',';
		}
		$sql = $sql . ')';
		$result = viper_dao::query($sql);
		if(!$result){
			die('Could not initalize global average table');
		}
	}
}
?>